<?php
/**
 * 后台附件相关操作控制器
 * 附件上传未公用原因前后台大多数上传需求不一样，所以分开处理
 * AttachmentController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author: huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class AttachmentController extends AdminController
{   
    protected $oModel;
    protected $_attributeLabels;
	public function __construct($id, $module) {
	    //模型
		$this->oModel = new AdminAttachment();
        $this->_attributeLabels = $this->oModel->attributeLabels();
		return parent::__construct($id, $module);
	}
	public function accessRules()
	{
		return array(
				array('allow',  // 所有人都允许访问的方法
						'actions'=>array('index'),
						'users'=>array('*'),
				),
				array('allow', // 允许登录用户访问的方法
						'actions'=>array('delete','upload','deleteall'),
						'users'=>array('@'),
				),
				array('deny',
						'users'=>array('*'),
				),
		);
	}
    /**
     * AttachmentController::actionIndex() 
     * 
     * @return void
     */
    public function actionIndex(){
        $aParams['pagesize'] = $this->pagesize;
		$aParams['page'] = $this->_request->getQuery('page' ,0);
		$aResult = $this->oModel->getList($aParams);
        $aResult['params'] = $aParams;	
		$this->render('index', $aResult);
    }
	/**
	 * AttachmentController::actionUpload() 
	 * 
	 * @return json 上传后的附件相关信息
	 */
	public function actionUpload(){
		//检查是否指定上传的模块
		$_POST['modtype'] = 'albumpic';
		//远文件名
		$attributes['oldName']=$_FILES['Filedata']['name'];
	    //后缀
	    $attributes['extensionName']=Yii::app()->common->getFilePostfix($_FILES['Filedata']['name']);
	    //大小
	    $attributes['filesize']=$_FILES['Filedata']['size'];
	    //相对路径(创建文件夹和文件名称)
	    $attributes['serverpath']=Yii::app()->common->getPathFileName($attributes['extensionName']);
	    //文件名称
	    $attributes['filename'] = $_FILES['Filedata']['name'];
	    //文件路径
	    $attributes['filepath']=Yii::app()->params['upload']['uploadview'].$attributes['serverpath'];
	    //判断文件类型是否在允许的范围内
        $aAllowType = explode('|' ,Yii::app()->params['upload']['upload_type']);
	    if(!in_array($attributes['extensionName'],$aAllowType)){
	        die(json_encode(array('status'=>0,'msg'=>'对不起！你上传的文件不再允许的范围内！')));
	    }
	    
	    //判断是通过本地还是ftp上传
	    if(Yii::app()->params['upload']['ftpenable'])
	    {//开启ftp上传
	        $ftp = Yii::app()->ftp;
	        $ftp->chdir('/');
	        $result=$ftp->put($attributes['serverpath'],$_FILES['Filedata']['tmp_name'],FTP_BINARY);
	        $attributes['location']='ftp';
	    }else
	    {//本地上传
	        $uploadlocal=Yii::app()->params['upload']['uploadlocal'].$attributes['serverpath'];
	        $result=@copy($_FILES['Filedata']['tmp_name'],$uploadlocal);
	        //Yii::app()->common->checkImageWH($uploadlocal);
	        $attributes['location']='0';
	    }
	    @unlink($_FILES['Filedata']['tmp_name']);
	    
	    if(!$result){
	        die(json_encode(array('status'=>0,'msg'=>'文件保存失败，请联系服务器管理员！')));
	    }
	    
	    $attributes['modtype']=isset($_POST['modtype'])?trim($_POST['modtype']):'default';
	    
	    
	    $return=Yii::app()->common->saveFile($attributes);
        
	    echo json_encode($return);exit;
	}
    /**
     * AttachmentController::actionDelete() 删除附件
     * 
     * @param mixed $aid
     * @return json 处理结果json
     */
    public function actionDelete($aid){
        $Attachment = BaseAttachmentModel::model()->findByPk($aid);
        if(!empty($Attachment) && $Attachment->delete()){
            //同时删除本地文件
            @unlink(Yii::app()->params['upload']['uploadlocal'].$Attachment->serverpath);
            $this->ajaxReturn(0,'删除成功');
        }else{
            $this->ajaxReturn(1,'删除失败');
        }
    }
    public function actionDeleteall(){
		$check=1;
		$ids=explode('|',$_POST['ids']);
		foreach ((array)$ids as $v){
			$result=$Attachment = BaseAttachmentModel::model()->findByPk($v);
			if($result && $result->delete()){
			 @unlink(Yii::app()->params['upload']['uploadlocal'].$result->serverpath);
				/* $oPower = AssignmentsModel::model()->find('userid=:userid' ,array(':userid'=>$v));
                if($oPower) $oPower->delete(); */
			}else{
				$check=0;
			}
		}
		if($check){
			$this->ajaxReturn(0,'删除成功');
		}else{
			$this->ajaxReturn(1,'删除失败');
		}
	}
}